package com.zhss.designpattern.strategy;

/**
 * @author fanwei
 * @desc 策略模式
 */
public class StrategryPatternDemo {

    interface DiscountCalculateStrategy{
        void calculate();
    }

    static class DiscountCalculateStrategyA implements DiscountCalculateStrategy{
        public void calculate() {
            System.out.println("执行优惠计价方式1的复杂业务逻辑");
        }
    }

    static class DiscountCalculateStrategyB implements DiscountCalculateStrategy{
        public void calculate() {
            System.out.println("执行优惠计价方式2的复杂业务逻辑");
        }
    }

    static class DiscountCalculateStrategyC implements DiscountCalculateStrategy{
        public void calculate() {
            System.out.println("执行优惠计价方式3的复杂业务逻辑");
        }
    }

    static class DiscountCalculateStrategyFactory{
        public static DiscountCalculateStrategy getDiscountCalculateStrategy(int style){
            if (style == 1){
               return new DiscountCalculateStrategyA();
            }else if (style == 2){
                return new DiscountCalculateStrategyB();
            }else {
                return new DiscountCalculateStrategyC();
            }
        }
    }

    static class Context{
        private DiscountCalculateStrategy strategy;

        public DiscountCalculateStrategy getStrategy() {
            return strategy;
        }

        public void setStrategy(DiscountCalculateStrategy strategy) {
            this.strategy = strategy;
        }

        public void calculate(){
            strategy.calculate();
        }
    }

    public static void main(String[] args) {
        int myStyle = 3;
        DiscountCalculateStrategy myStrategy = DiscountCalculateStrategyFactory.getDiscountCalculateStrategy(myStyle);
        Context context = new Context();
        context.setStrategy(myStrategy);
        context.calculate();
    }
}
